package com.winit.agent.repository;

import com.winit.agent.model.Agent;
import com.winit.agent.model.AgentWorkspace;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

@Repository
public class AgentWorkspaceRepositoryExtImpl implements IAgentWorkspaceRepositoryExt{

    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    @Override
    public boolean updateByWsIdForPropertyNotNull(AgentWorkspace agentWorkspace) {
        if (StringUtils.isEmpty(agentWorkspace.getWsId())){
            return false;
        }
        AgentWorkspace entity = this.entityManager.find(AgentWorkspace.class, agentWorkspace.getWsId());
        if (agentWorkspace.getAgentId() != null){
            entity.setAgentId(agentWorkspace.getAgentId());
        }

        if (agentWorkspace.getIsDefault() != null){
            entity.setIsDefault(agentWorkspace.getIsDefault());
        }

        if (agentWorkspace.getWsCode() != null){
            entity.setWsCode(agentWorkspace.getWsCode());
        }

        if (agentWorkspace.getPrdId() != null){
            entity.setPrdId(agentWorkspace.getPrdId());
        }

        if (agentWorkspace.getWsPath() != null){
            entity.setWsPath(agentWorkspace.getWsPath());
        }

        if (agentWorkspace.getWsPort() != null){
            entity.setWsPort(agentWorkspace.getWsPort());
        }
        entityManager.merge(entity);
        entityManager.flush();
        return true;
    }

}
